package com.kaizeli.website.controller;

import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.impl.EmployeeDynamicImportServiceImpl;
import com.kaizeli.website.service.impl.ProjectDynamicImportServiceImpl;
import com.kaizeli.website.service.impl.ContractPersonnelDynamicImportServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 动态导入控制器
 * 支持智能表头匹配，无需固定列顺序
 */
@Slf4j
@RestController
@RequestMapping("/api/dynamic-import")
@SecurityRequirement(name = "Bearer Authentication")
@Tag(name = "智能导入接口", description = "支持表头自动匹配的智能导入")
public class DynamicImportController {
    
    @Autowired
    private EmployeeDynamicImportServiceImpl employeeDynamicImport;
    
    @Autowired
    private ProjectDynamicImportServiceImpl projectDynamicImport;
    
    @Autowired
    private ContractPersonnelDynamicImportServiceImpl contractPersonnelDynamicImport;
    
    // ==================== 员工导入 ====================
    
    @PostMapping("/employee")
    @Operation(summary = "智能导入员工（新增模式）", description = "支持表头自动匹配，已存在则跳过")
    public Result<Map<String, Object>> importEmployee(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = employeeDynamicImport.importEmployee(file);
            Boolean success = (Boolean) result.get("success");
            if (Boolean.TRUE.equals(success)) {
                return Result.success(result);
            } else {
                return Result.error(500, result.get("message").toString());
            }
        } catch (Exception e) {
            log.error("导入失败", e);
            return Result.error(500, "导入失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/employee-update")
    @Operation(summary = "智能导入员工（更新模式）", description = "支持表头自动匹配，已存在则更新")
    public Result<Map<String, Object>> importEmployeeWithUpdate(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = employeeDynamicImport.importEmployeeWithUpdate(file);
            Boolean success = (Boolean) result.get("success");
            if (Boolean.TRUE.equals(success)) {
                return Result.success(result);
            } else {
                return Result.error(500, result.get("message").toString());
            }
        } catch (Exception e) {
            log.error("导入失败", e);
            return Result.error(500, "导入失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/employee/help")
    @Operation(summary = "获取员工导入说明", description = "获取员工字段映射和导入说明")
    public Result<Map<String, Object>> getEmployeeImportHelp() {
        return Result.success(employeeDynamicImport.getImportHelp());
    }
    
    // ==================== 项目导入 ====================
    
    @PostMapping("/project")
    @Operation(summary = "智能导入项目（新增模式）", description = "支持表头自动匹配，已存在则跳过")
    public Result<Map<String, Object>> importProject(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = projectDynamicImport.importProject(file);
            Boolean success = (Boolean) result.get("success");
            if (Boolean.TRUE.equals(success)) {
                return Result.success(result);
            } else {
                return Result.error(500, result.get("message").toString());
            }
        } catch (Exception e) {
            log.error("导入失败", e);
            return Result.error(500, "导入失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/project-update")
    @Operation(summary = "智能导入项目（更新模式）", description = "支持表头自动匹配，已存在则更新")
    public Result<Map<String, Object>> importProjectWithUpdate(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = projectDynamicImport.importProjectWithUpdate(file);
            Boolean success = (Boolean) result.get("success");
            if (Boolean.TRUE.equals(success)) {
                return Result.success(result);
            } else {
                return Result.error(500, result.get("message").toString());
            }
        } catch (Exception e) {
            log.error("导入失败", e);
            return Result.error(500, "导入失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/project/help")
    @Operation(summary = "获取项目导入说明", description = "获取项目字段映射和导入说明")
    public Result<Map<String, Object>> getProjectImportHelp() {
        return Result.success(projectDynamicImport.getImportHelp());
    }
    
    // ==================== 合同人员导入 ====================
    
    @PostMapping("/contract-personnel")
    @Operation(summary = "智能导入合同人员（新增模式）", description = "支持表头自动匹配，已存在则跳过")
    public Result<Map<String, Object>> importContractPersonnel(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = contractPersonnelDynamicImport.importContractPersonnel(file);
            Boolean success = (Boolean) result.get("success");
            if (Boolean.TRUE.equals(success)) {
                return Result.success(result);
            } else {
                return Result.error(500, result.get("message").toString());
            }
        } catch (Exception e) {
            log.error("导入失败", e);
            return Result.error(500, "导入失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/contract-personnel-update")
    @Operation(summary = "智能导入合同人员（更新模式）", description = "支持表头自动匹配，已存在则更新")
    public Result<Map<String, Object>> importContractPersonnelWithUpdate(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = contractPersonnelDynamicImport.importContractPersonnelWithUpdate(file);
            Boolean success = (Boolean) result.get("success");
            if (Boolean.TRUE.equals(success)) {
                return Result.success(result);
            } else {
                return Result.error(500, result.get("message").toString());
            }
        } catch (Exception e) {
            log.error("导入失败", e);
            return Result.error(500, "导入失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/contract-personnel/help")
    @Operation(summary = "获取合同人员导入说明", description = "获取合同人员字段映射和导入说明")
    public Result<Map<String, Object>> getContractPersonnelImportHelp() {
        return Result.success(contractPersonnelDynamicImport.getImportHelp());
    }
}

